package com.base.vistter.iframe.template;

import com.base.vistter.iframe.constant.FileConstant;
import com.base.vistter.iframe.exception.IframeException;
import com.base.vistter.iframe.properties.MinioProperties;
import io.minio.*;
import io.minio.messages.Bucket;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.util.List;

@Slf4j
@Component
@Configuration
@EnableConfigurationProperties(MinioProperties.class)
public class MinioTemplate {

    @Resource(name = "minioProperties")
    private MinioProperties minioProperties;

    private MinioClient minioClient;

    public MinioTemplate() {

    }

    public MinioClient getMinioClient() {
        if (this.minioClient == null) {
            this.minioClient = MinioClient.builder().endpoint(minioProperties.getUrl(), minioProperties.getPort(), false).credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey()).build();
        }
        return this.minioClient;
    }

    /**
     * 桶子列表
     * @return
     * @throws IframeException
     */
    public List<Bucket> listBuckets() throws IframeException {
        try {
            return this.getMinioClient().listBuckets();
        } catch (Exception e) {
            throw new IframeException(e.getMessage());
        }
    }

    /**
     * 判断桶子是否存在
     * @param bucketName
     * @return
     * @throws IframeException
     */
    public Boolean bucketExists(String bucketName) throws IframeException{
        try {
            return this.getMinioClient().bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        }catch (Exception e){
            throw new IframeException(e.getMessage());
        }
    }

    /**
     * 创建桶子
     * @param bucketName
     * @throws IframeException
     */
    public void makeBucket(String bucketName) throws IframeException {
        try {
            if(!this.bucketExists(bucketName)){
                this.getMinioClient().makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
            }
        }catch (Exception e){
            throw new IframeException(e.getMessage());
        }
    }

    /**
     * 删除桶子
     * @param bucketName
     * @throws IframeException
     */
    public void removeBucket(String bucketName) throws IframeException{
        try {
            if(this.bucketExists(bucketName)){
                this.getMinioClient().removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
            }
        }catch (Exception e){
            throw new IframeException(e.getMessage());
        }
    }

    /**
     * 上传文件
     * @param bucketName 桶子
     * @param filePath 文件路径
     * @param stream 文件流
     * @throws IframeException
     */
    public void uploadFile(String bucketName , String filePath , InputStream stream) throws IframeException{
        try {
            if(!this.bucketExists(bucketName)){
                this.makeBucket(bucketName);
            }
            this.getMinioClient().putObject(PutObjectArgs.builder().bucket(bucketName).object(filePath).stream(stream , stream.available(), -1).build());
        }catch (Exception e){
            throw new IframeException(e.getMessage());
        }
    }

    /**
     * 下载文件
     * @param filePath 文件路径，包括桶子
     * @return
     * @throws IframeException
     */
    public InputStream downloadFile(String filePath) throws IframeException{
        try {
            String bucketName = filePath.substring(0, filePath.indexOf(FileConstant.SPRIT));
            filePath = filePath.substring(filePath.indexOf(FileConstant.SPRIT) + 1);
            return this.getMinioClient().getObject(GetObjectArgs.builder().bucket(bucketName).object(filePath).build());
        }catch (Exception e){
            log.info(e.getMessage());
            throw new IframeException(e.getMessage());
        }
    }
}
